using Granite.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Granite.Commons.Tests.Collections
{
    [TestClass()]
    public class ObservingCollectionTests
    {

        //[TestMethod()]
        //public void ObservingCollection_MemoryTest()
        //{
        //    var source = new ExtendedObservableCollection<int>();
        //    var target = source.ObserveWithConversion(x => x + 5);
        //    var wr = new WeakReference(target);

        //    var sourceEvents = new Queue<NotifyCollectionChangedEventArgs>();
        //    var targetEvents = new Queue<NotifyCollectionChangedEventArgs>();

        //    var sourceEvents2 = new Queue<PropertyChangedEventArgs>();
        //    var targetEvents2 = new Queue<PropertyChangedEventArgs>();

        //    source.CollectionChanged += (s, e) => sourceEvents.Enqueue(e);
        //    target.CollectionChanged += (s, e) => targetEvents.Enqueue(e);

        //    source.PropertyChanged += (s, e) => sourceEvents2.Enqueue(e);
        //    target.PropertyChanged += (s, e) => targetEvents2.Enqueue(e);

        //    GCCycle();

        //    source.Add(1);
        //    source.Add(2);
        //    source.Add(3);

        //    GCCycle();

        //    Assert.AreEqual(3, sourceEvents.Count);
        //    Assert.AreEqual(3, targetEvents.Count);
        //    Assert.AreEqual(6, sourceEvents2.Count);
        //    Assert.AreEqual(6, targetEvents2.Count);

        //    Assert.AreEqual(source[0] + 5, target[0]);
        //    Assert.AreEqual(source[1] + 5, target[1]);
        //    Assert.AreEqual(source[2] + 5, target[2]);

        //    target = null;

        //    GCCycle();


        //    Assert.IsFalse(wr.IsAlive);

        //}

		/*

	        [TestMethod()]
        public void ObserveWithConversionTest()
        {

            var source = new ObservableCollection<int>();
            Func<int, string> translate = (i) => i.ToString();

            source.Add(1);
            source.Add(2);
            source.Add(3);

            var target = source.ObserveWithConversion(translate);

            Assert.AreEqual("1", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("3", target[2]);
            Assert.AreEqual(3, target.Count);

            source.Add(4);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual(4, target.Count);

            source.Remove(4);
            Assert.AreEqual("1", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("3", target[2]);
            Assert.AreEqual(3, target.Count);

            source.Clear();
            Assert.AreEqual(0, target.Count);

            source.Add(1);
            source.Add(2);
            source.Add(3);
            Assert.AreEqual("1", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("3", target[2]);

            source[0] = 10;
            Assert.AreEqual("10", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("3", target[2]);


        }


        [TestMethod()]
        public void LinkTest()
        {

            var source = new ObservableCollection<int>();
            Func<int, string> convert = (i) => i.ToString();
            Func<string, int> convertBack = (s) => Int32.Parse(s);

            source.Add(1);
            source.Add(2);
            source.Add(3);

            var target = source.Link(convert, convertBack);

            Assert.AreEqual("1", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("3", target[2]);
            Assert.AreEqual(3, target.Count);

            source.Add(4);
            Assert.AreEqual("4", target[3]);
            Assert.AreEqual(4, target.Count);

            source.Remove(4);
            Assert.AreEqual("1", target[0]);
            Assert.AreEqual("2", target[1]);
            Assert.AreEqual("3", target[2]);
            Assert.AreEqual(3, target.Count);

            source.Clear();
            Assert.AreEqual(0, target.Count);
            Assert.AreEqual(0, source.Count);

            target.Add("1");
            target.Add("2");
            target.Add("3");
            Assert.AreEqual(1, source[0]);
            Assert.AreEqual(2, source[1]);
            Assert.AreEqual(3, source[2]);
            Assert.AreEqual(3, source.Count);
        }

		 */

        private static void GCCycle()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
}
